var Listm = []
import Mock from 'mockjs'
// 拓展mockjs
Mock.Random.extend({
  // 订单编号
  phone: function () {
    var phonePrefixs = ['2019', '2020', '2021', '2022'] // 自己写前缀哈
    return this.pick(phonePrefixs) + Mock.mock(/\d{12}/) // Number()
  },
  biaoti: function () {
    var phonePrefixs = ['幼儿英语课程', '新幼儿英语', '我爱学英语'] // 自己写前缀哈
    return this.pick(phonePrefixs) // Number()
  },
  zhuangtai: function () {
    var phonePrefixs = ['待支付', '已支付'] // 自己写前缀哈
    return this.pick(phonePrefixs) // Number()
  },
  telphone: function() {
    var phonePrefixs = ['132', '135', '189', '177', '186', '134', '159'] // 自己写前缀哈
    return this.pick(phonePrefixs) + Mock.mock(/\d{8}/) // Number()
  }
})
for (var i = 0; i < 100; i++) {
  Listm.push({
    tel: Mock.Random.phone(),
    wechat: Mock.mock('@cname()'),
    biaoti: Mock.Random.biaoti(),
    pay: Mock.mock('@integer(1000, 3000)'),
    zhuangtai: Mock.Random.zhuangtai(),
    createTime: Mock.mock('@datetime'),
    telphone: Mock.Random.telphone(),
  })
}
import { param2Obj } from '../utils/utilsDemo'

export default {
  getMoneyData: (config) => { //查询接口返回的数据
    // console.log('config', config);//post请求参数在body中，get请求在url上
    // console.log(param2Obj(config.url));
    // 查询逻辑
    var { keyword, page } = param2Obj(config.url)
    var newARr = Listm.filter(item => item.tel.indexOf(keyword) != -1)
    var pageResult = newARr.filter((item, index) => {
      return (page - 1) * 10 <= index && index < page * 10
    })
    return {
      code: 200,
      msg: '请求成功',
      data: pageResult, // 分页后数据
      total: Listm.length // 总条数
    }
  },

  batchDeleteData: config => { // 批量删除的接口数据
    // console.log('config', config.body)
    var { tel } = JSON.parse(config.body)
    // 删除逻辑
    var newArr = Listm.filter(item => tel.indexOf(item.tel) == -1)
    Listm = newArr
    return {
      code: 200,
      msg: '批量删除成功',
      data: null
    }
  },

  updateData: config => {
    //   修改逻辑
    var tel = JSON.parse(config.body).tel
    var { tel,wechat, telphone, biaoti, pay, zhuangtai,createTime } = JSON.parse(config.body)
    Listm.forEach(item => {
      if (item.tel == tel) {
        // item就是我们查找的旧的值
        item.tel = tel
        item.wechat = wechat
        item.telphone = telphone
        item.biaoti = biaoti
        item.pay = pay
        item.zhuangtai = zhuangtai
        item.createTime = createTime
      }
    })
    return {
      code: 200,
      msg: '修改成功',
      data: null
    }
  }

}
